'use strict';

angular.module('app')
	.controller('userAdvertsController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '广告主用户';
        $scope.param = { };
		// $scope.platforms=[];
        $scope.loading = false;
		$scope.search = function () {
	        $scope.loading = true;
            var regStartTime = $("#regStartTime").val();
            var regEndTime = $("#regEndTime").val();
            var regEndTime = $("#regEndTime").val();

            $scope.param.regStartTime = regStartTime;
            $scope.param.regEndTime = regEndTime;
            var id = $("#id").val();
            $scope.param.id = id;

            var approveStartTime = $("#approveStartTime").val();
            var approveEndTime = $("#approveEndTime").val();
            $scope.param.approveStartTime = approveStartTime;
            $scope.param.approveEndTime = approveEndTime;
			$.ajax({
                url : $scope.app.project_name + '/userAdverts/read/list',
                data: $scope.param
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;
                    console.log(result.data);
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
		}

		$scope.selectmodal = function (zzyAdvId) {

           	 $.ajax({
                url : $scope.app.project_name + '/userAdverts/read/getPlatformList',
                data: {'zzyAdvId': zzyAdvId}
            }).then(function(result) {
                console.log("=======");
                if (result.httpCode == 200) {
                	$scope.$apply(function () {
                        $scope.selectPlatform = true;
                        $scope.platforms = result.data;
                    })

                } else {
                    $scope.msg = result.msg;
                }
            });

		}

		$scope.searchRe = function () {
			$scope.loading = true;
			$scope.param.pageNum = 1;

			var regStartTime = $("#regStartTime").val();
			var regEndTime = $("#regEndTime").val();
			$scope.param.regStartTime = regStartTime;
			$scope.param.regEndTime = regEndTime;

			var approveStartTime = $("#approveStartTime").val();
			var approveEndTime = $("#approveEndTime").val();
			$scope.param.approveStartTime = approveStartTime;
			$scope.param.approveEndTime = approveEndTime;
            var id = $("#id").val();
            $scope.param.id = id;
			$.ajax({
				url : $scope.app.project_name + '/userAdverts/read/list',
				data: $scope.param
			}).then(function(result) {
				$scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.selected = [];
		var updateSelected = function (action, contact) {
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}

			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};

		$scope.updateSelection = function ($event, contact) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}
			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};

		// 全选操作
        $scope.selectAll = function ($event) {
        	var checkbox = $event.target;
        	var action = (checkbox.checked ? 'add' : 'remove');
        	
        	for (var i = 0; i < $scope.pageInfo.records.length; i++) {
        		var contact = $scope.pageInfo.records[i];
        		updateSelected(action, contact);
        	}
        };
        
        $scope.isSelected = function (id) {
        	return $scope.selected.indexOf(id) >= 0;
        };

		$scope.approve = function(id, state) {

            var ids = [];

            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }

            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要审核的记录"});
                return;
            }

            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.status == 1 && item.checked){
                    count++;
                }else if(item.checked && item.status != 1) {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }

            $scope.alertMessage({
                text: "确定做此操作吗？", //内容
                btnCancel: true, //取消按钮
                btnConfirm: true //确认按钮
            }).then(function () {
                console.log("===========");
                $("#btn").attr("disabled", true);
                var ids = $scope.selected;
                var idStr = ids.join(',');
                $.ajax({
                    url: $scope.app.project_name + '/userAdverts/update/approve',
                    data: {'ids': idStr, 'state': state},
                    traditional: true
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.selected = [];
                        $scope.alertModal({"content":"操作成功"});
                    } else {
                        $scope.msg = result.msg;
                    }
                    $("#btn").attr("disabled", false)
                    $scope.search();
                    $scope.$apply();
                });

            })
		}




 $scope.refuseAdverts = function (id, state) {
			$scope.userAdverts = {};
			var ids = [];

			if (id == null || id == '') {
				ids = $scope.selected;
			} else {
				ids.push(id);
			}

			var idStr = ids.join(',');
			if (idStr == null || idStr == '') {
				$scope.alertModal({"content":"请选择需要审核的记录"});
				return;
			} else {

                //判断是否有待审核的并记录有多少条
                var count = 0;
                var nocount = 0;
                angular.forEach($scope.pageInfo.records,function (item) {
                    if(item.status == 1 && item.checked){
                        count++;
                    }else if(item.checked && item.status != 1) {
                        nocount++;
                    }
                })
                if(nocount>0){
                    $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                    return;
                }

				$scope.userAdverts.ids = idStr;
				$scope.userAdverts.state = state;

				$('#refuseUserAdverts').modal('show', true);
			}
		}

		$scope.saveUserAdverts = function (state) {
			var approveReason = $("#approveReason").val();
			var utmSource = $("#utmSource").val();
            var ids = $scope.selected;
            var idStr = ids.join(',');
			if (approveReason == "" || approveReason == null) {
                $scope.alertModal({"content":"审核失败原因不能为空"});
                $('#refuseUserAdverts').modal('hide');
				return;
			}
			$.ajax({
				url : $scope.app.project_name + '/userAdverts/update/approve',
                data: {'ids': idStr, 'state' : state, 'approveReason' : approveReason, 'utmSource' : utmSource}
			}).then(function(result) {
				$scope.loading = false;
				if (result.httpCode == 200) {

					$scope.alertModal({"content":"操作成功"});
					$scope.param.keyword = null;
					$scope.selected = [];
					$scope.search();
				} else {
					$scope.alertModal({"content":"操作失败"});
				}
                $('#refuseUserAdverts').modal('hide');
                $('#refuseUserAdverts').val('');
                $scope.$apply();
			});
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };

		$scope.businessLicenses = [];
		$scope.preview = function(businessLicense){
			if (businessLicense != null && businessLicense != '') {
				$scope.businessLicenses = businessLicense.split('|');
			}
			$scope.firstApply = true;
		}
		$scope.copySuppy=function(){
            var ids =  $scope.selected;
            var idStr = ids.join(',');
            if(ids.length>1){
                $scope.alertModal({"content":"每次只能开通一个流量主"});
                return ;
            }
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择要开通的广告主"});
                return ;
            }
          /*  var count = 0;

            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.status != 2 && item.checked){
                    count ++;
                }
            })

            if(count>0){
                $scope.alertModal({"content":"未审核通过的广告主无法开通流量主"});
                return ;
            }*/
            $scope.alertMessage({
                text: "确认开通流量主？", //内容
                btnCancel: true, //取消按钮
                btnConfirm: true //确认按钮
            }).then(function () {
                console.log("===========");
                $("#btn").attr("disabled", true);

                $.ajax({
                    url: $scope.app.project_name + '/userAdverts/update/copySuppy',
                    data: {'ids': idStr},
                    traditional: true
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.selected = [];
                        if (result.data>=0){
                            $scope.alertModal({"content":"操作成功"});
                        }else{
                            $scope.alertModal({"content":"该用户信息不全，无法开通流量主"});
                        }
                    } else {
                        $scope.msg = result.msg;
                    }
                    $("#btn").attr("disabled", false)
                    $scope.search();
                    $scope.$apply();
                });

            })

        }

} ]);